package com.csmz.mybookreadernew.UI.Fragment.MyReader;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.AdapterView;
import android.widget.ListView;

import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.Utils;
import com.csmz.mybookreadernew.Adapter.ListViewAdapter;
import com.csmz.mybookreadernew.Bean.RssItemBean;
import com.csmz.mybookreadernew.DBSQLite.DbMySQLite;
import com.csmz.mybookreadernew.R;
import com.csmz.mybookreadernew.RssUtils.RssFeed;
import com.csmz.mybookreadernew.RssUtils.RssItem;
import com.csmz.mybookreadernew.UI.Activity.WebViewActivity;
import com.king.base.BaseFragment;

import java.util.ArrayList;
import java.util.List;

public class MyNewsReadFragment_00 extends BaseFragment implements AdapterView.OnItemClickListener {
    private ListView itemList;
    private static RssFeed feed = null;
    private static Context context;
    private List<RssItemBean> rssItemBeans;
    public static void getRssFeed(Context c,RssFeed f){
        feed=f;
        context=c;
    }

    @Override
    public int inflaterRootView() {
        return R.layout.fragment_my_news;
    }

    @Override
    public void initUI() {
        showListView();
    }

    /*
     * 把RSS内容绑定到ui界面进行显示
     */
    private void showListView() {
        itemList=findView(R.id.list);
        if (feed == null) {
            setFeedData();
        }else {
            ListViewAdapter adapter=new ListViewAdapter(getActivity(),feed);
            itemList.setAdapter(adapter);
            itemList.setOnItemClickListener(this);
            itemList.setSelection(0);
        }
    }

    private void setFeedData() {
//        DbMySQLite dbMySQLite=new DbMySQLite(context);
//        rssItemBeans = new ArrayList<>();
//        rssItemBeans=dbMySQLite.findAllNewInfo();
//       if (rssItemBeans!=null){
//           for (int i =0;i<rssItemBeans.size();i++){
//               RssItem item=new RssItem(rssItemBeans.get(i).getTitle(),rssItemBeans.get(i).getPubDate(),rssItemBeans.get(i).getLink());
//               feed.addItem(item);
//           }
//       }
        feed=new RssFeed();
        RssItem item01=new RssItem("习近平在首届中国国际进口博览会开幕式上发表主旨演讲", "2018-11-05 16:43:47", "http://news.qq.com/a/20181105/012446.htm");
        feed.addItem(item01);
        RssItem item02=new RssItem("国际锐评：共建开放型世界经济 中国不断注入新动力","2018-11-05 15:05:20","http://news.qq.com/a/20181105/011049.htm");
        feed.addItem(item02);
        RssItem item03=new RssItem("全媒风向 | Facebook重组Oculus","2018-11-05 14:50:52","http://news.qq.com/a/20181105/010740.htm");
        feed.addItem(item03);
        RssItem item04=new RssItem("习近平：中国国际进口博览会不仅年年办 还要越办越好","2018-11-05 14:32:16","http://news.qq.com/a/20181105/010308.htm");
        feed.addItem(item04);
        RssItem item05=new RssItem("一展汇世界——写在首届中国国际进口博览会开幕之际","2018-11-05 13:05:28","http://news.qq.com/a/20181105/008789.htm");
        feed.addItem(item05);
        RssItem item06=new RssItem("习近平：各国应努力改进营商环境 不能像手电筒只照他人不照自己","2018-11-05 12:38:31","http://news.qq.com/a/20181105/008646.htm");
        feed.addItem(item06);
        RssItem item07=new RssItem("习近平主席将与中外领导人一起参观国家馆","2018-11-05 12:38:01","http://news.qq.com/a/20181105/008644.htm");
        feed.addItem(item07);
        RssItem item08=new RssItem("习近平：对中国经济前景可抱乐观态度 中国经济三个“没有改变”","2018-11-05 12:37:53","http://news.qq.com/a/20181105/008643.htm");
        feed.addItem(item08);
        RssItem item09=new RssItem("习近平在首届中国国际进口博览会开幕式上的主旨演讲（全文）","2018-11-05 12:35:55","http://news.qq.com/a/20181105/008627.htm");
        feed.addItem(item09);
        RssItem item10=new RssItem("习近平出席首届中国国际进口博览会开幕式并发表主旨演讲","2018-11-05 12:27:10","http://news.qq.com/a/20181105/008584.htm");
        feed.addItem(item10);
        RssItem item11=new RssItem("习近平：中国开放大门只会越开越大 强调三个“不会停滞”","2018-11-05 12:05:12","http://news.qq.com/a/20181105/008338.htm");
        feed.addItem(item11);
        ListViewAdapter adapter=new ListViewAdapter(getActivity(),feed);
        itemList.setAdapter(adapter);
        itemList.setOnItemClickListener(this);
        itemList.setSelection(0);
    }

    @Override
    public void initData() {

    }

    @Override
    public void addListeners() {

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        SPUtils.getInstance("Url").put("link",feed.getItem(position).getLink());
        startActivity(new Intent(getActivity(),WebViewActivity.class));
//        view=getLayoutInflater().inflate(R.layout.activity_show_web,null);
//        WebView web_view = view.findViewById(R.id.web_view);
//        web_view.loadUrl(feed.getItem(position).getLink());
    }
}
